#!/usr/bin/env python
# coding: utf-8

# In[ ]:
import os

import inspect
# 获取目录绝对路径（由于代码需要线上部署，既不能写相对路径，绝对路径也不能写死）
path = inspect.getfile(inspect.currentframe())
abspath = os.path.dirname(os.path.abspath(path))


class Instructions:
    def judgeSentence(self, sentence):
        from pyhanlp import HanLP
        import json
        with open(abspath + '/InstructionSet.json', 'r') as InstructionSet:
            instructionSet = json.load(InstructionSet)
        result1 = HanLP.parseDependency(sentence)
        attribute = ""
        result = False
        for index, word in enumerate(result1.iterator()):
            if (word.LEMMA in instructionSet[-1]['content']):
                result = True
        return result

    def processSentence(self, sentence):
        #     result = re.split('，|。| |？', sentence)
        #     sentence = result[-1]
        from pyhanlp import JClass
        from pyhanlp import HanLP
        import json
        with open(abspath + '/InstructionSet.json', 'r') as InstructionSet:
            instructionSet = json.load(InstructionSet)
        CustomDictionary = JClass("com.hankcs.hanlp.dictionary.CustomDictionary")
        CustomDictionary.add("从大到小")
        CustomDictionary.add("合计值")
        CustomDictionary.add("大屏")
        CustomDictionary.add("柱图")
        CustomDictionary.add("饼图")
        CustomDictionary.add("合同金额")
        CustomDictionary.add("升序")
        CustomDictionary.add("合同收益")
        CustomDictionary.add("合同盈利")
        CustomDictionary.add("年")
        CustomDictionary.add("第一列")
        sentence = sentence.replace("年", "年份")
        #         print("初次过滤的句子：" + sentence)
        #         print()
        terms = HanLP.segment(sentence)
        for term in terms:
            pass
            # 词和词性from pyhanlp import *
        #             print(term.word,term.nature)
        #         print()
        result1 = HanLP.parseDependency(sentence)
        #         print(result1)
        #         print()
        #         print("以下是处理结果")
        attribute = ""
        result = []
        for index, word in enumerate(result1.iterator()):  # 通过dir()可以查看sentence的方法
            if (word.DEPREL == "定中关系" and attribute != ""):
                attribute = attribute + word.LEMMA
            if (word.DEPREL == "定中关系" and attribute == ""):
                attribute = word.LEMMA
            if (word.DEPREL != "定中关系"):
                if (attribute != ""):
                    #                     print(attribute)
                    result.append(attribute)
                    #                     print()
                    attribute = ""
            if (
                    word.DEPREL == "核心关系" or word.DEPREL == "动宾关系" or word.DEPREL == "介宾关系" or word.DEPREL == "并列关系" or word.DEPREL == "主谓关系"):
                #                 print(word.LEMMA)
                result.append(word.LEMMA)
        #                 print()
        #         print()
        #         print(result)
        for index, word in enumerate(result):
            if (word == "前面" or word == "前"):
                result[index] = "FRONT"
            if (word == "后面" or word == "后"):
                result[index] = "BEHIND"
            if (word == "上面" or word == "上"):
                result[index] = "UP"
            if (word == "下面" or word == "下"):
                result[index] = "DOWN"
            if (word in instructionSet[0]['content']):
                del result[index]
                result.insert(0, instructionSet[0]['type'])
            if (word in instructionSet[1]['content']):
                del result[index]
                result.insert(0, instructionSet[1]['type'])
            if (word in instructionSet[2]['content']):
                del result[index]
                result.insert(0, instructionSet[2]['type'])
            if (word in instructionSet[3]['content']):
                del result[index]
                result.insert(0, instructionSet[3]['type'])
            if (word in instructionSet[4]['content']):
                del result[index]
                result.insert(0, instructionSet[4]['type'])
            if (word in instructionSet[5]['content']):
                del result[index]
                result.insert(0, instructionSet[5]['type'])
        return result

